package com.zzedu.web.controller;

import com.zzedu.common.annotation.Log;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.enums.OperatorType;
import com.zzedu.device.req.BindDevice;
import com.zzedu.device.req.LineDevice;
import com.zzedu.device.req.Login;
import com.zzedu.device.req.LoginDevice;
import com.zzedu.device.rsp.ApiLoginVO;
import com.zzedu.device.rsp.LoginDeviceVO;
import com.zzedu.enmuns.ConfigEnums;
import com.zzedu.manager.IZzAppDeviceManager;
import com.zzedu.result.Result;
import com.zzedu.result.ResultEnum;
import com.zzedu.system.service.ISysConfigService;
import com.zzedu.utils.BaseReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 大屏
 */
@RestController
public class AppDeviceController {

    @Autowired
    IZzAppDeviceManager zzAppDeviceManager;

    @Autowired
    ISysConfigService sysConfigService;

    /**
     * 设备登录
     *
     * @return
     */
    @PostMapping(value = {"/login"}, produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
    @Log(title = "大屏-登录", businessType = BusinessType.APP_DEVICE_TEACHER, operatorType = OperatorType.MOBILE)
    public Result<LoginDeviceVO> login(@Validated @RequestBody BaseReq<LoginDevice> req) {
        return zzAppDeviceManager.login(req.getData());
    }

    /**
     * 绑定大屏设备
     *
     * @return
     */
    @PostMapping(value = {"/bindDevice"}, produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
    @Log(title = "大屏-绑定", businessType = BusinessType.APP_DEVICE_TEACHER, operatorType = OperatorType.MOBILE)
    public Result bindDevice(@Validated @RequestBody BaseReq<BindDevice> req) {
        return zzAppDeviceManager.bindDevice(req.getData());
    }

    /**
     * 获取版本号
     *
     * @param baseRequest
     * @return
     */
    @PostMapping("/getVersion")
    public Result<String> getVersion(@Validated @RequestBody BaseReq baseRequest) {
        Result<String> result = new Result<>();
        result.setResultMessage(ResultEnum.SUCCESS.getDesc());
        result.setResultCode(ResultEnum.SUCCESS.getCode());
        result.setData(sysConfigService.selectConfigByKeyCache(ConfigEnums.DEVICE_VERSION.getCode()));
        return result;
    }

    /**
     * 定时请求-计算在线时长
     * @param baseRequest
     * @return
     */
    @PostMapping("/lineTime")
    public Result lineTime(@Validated @RequestBody BaseReq<LineDevice> req){
        return  zzAppDeviceManager.lineTime(req.getData());
    }

    /**
     * 小程序登录
     *
     * @return
     */
    @PostMapping(value = {"/wxMiniLogin"}, produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
    @Log(title = "小程序登录", businessType = BusinessType.APP_DEVICE_TEACHER, operatorType = OperatorType.MOBILE)
    public Result<ApiLoginVO> wxMiniLogin(@Validated @RequestBody BaseReq<Login> req) {
        return zzAppDeviceManager.wxMiniLogin(req.getData());
    }
}
